package com.jozen.smart_toilet_system.tagView;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.jozen.modelbase.netwoek.bean.SmartToiletInterfaceConfModel;
import com.jozen.modelbase.netwoek.util.BigDecimalUtil;
import com.jozen.modelbase.netwoek.util.DensityUtil;
import com.jozen.smart_toilet_system.ui.common_tag_view.CommonTagView;

/**
 * 厕间展示视图
 */
public class ToiletRoomTagShowLayout extends RelativeLayout {
    private TagViewCallBack tagViewCallBack;
    private SmartToiletInterfaceConfModel ztConfigurationModel;

    public ToiletRoomTagShowLayout(Context context) {
        super(context, null);
    }

    public ToiletRoomTagShowLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public ToiletRoomTagShowLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    /**
     * 组件点击回调
     *
     * @param tagViewCallBack
     */
    public void setTagViewCallBack(TagViewCallBack tagViewCallBack) {
        this.tagViewCallBack = tagViewCallBack;
    }

    private long materialUpdateTime = 0;//广告轮播图更新时间
    private String signature = "unique_signature";

    public synchronized void setData(SmartToiletInterfaceConfModel model, int width, int height, int containerW, int containerH) {
        ztConfigurationModel = model;
        if (ztConfigurationModel != null
                && ztConfigurationModel.getControls() != null
                && ztConfigurationModel.getControls().size() > 0) {
            removeAllViews();
            invalidate();
            for (int i = 0; i < ztConfigurationModel.getControls().size(); i++) {

                int tempX = ztConfigurationModel.getControls().get(i).getX();
                int tempY = ztConfigurationModel.getControls().get(i).getY();

                double centerPointX = BigDecimalUtil.divide(tempX, DensityUtil.px2dip(getContext(), containerW), 4);
                double centerPointY = BigDecimalUtil.divide(tempY, DensityUtil.px2dip(getContext(), containerH), 4);

                int tempWidth = DensityUtil.px2dip(getContext(), width);
                int tempHeight = DensityUtil.px2dip(getContext(), height);

                int x = (int) (centerPointX * tempWidth);
                int y = (int) (centerPointY * tempHeight);


                ztConfigurationModel.getControls().get(i).setTag(x + "tag" + y);//用作标识用来跳转

                if (model.getToiletIconUpdateTime() > materialUpdateTime) {
                    materialUpdateTime = model.getToiletIconUpdateTime();
                    signature = "unique_signature_refresh";
                } else {
                    signature = "unique_signature";
                }
                showTagView(x, y, ztConfigurationModel.getControls().get(i), signature);
            }
        } else {
            removeAllViews();
            invalidate();
        }
    }

    private void showTagView(int x, int y, SmartToiletInterfaceConfModel.ListDto bean, String signature) {
        View view = null;
        LayoutParams params = new LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);

//        params.leftMargin = x - 10;
        params.leftMargin = x;

        view = new CommonTagView(getContext());

//        params.topMargin = y - 5;
        params.topMargin = y;
        //上下位置在视图内
        if (params.topMargin < 0) {
            params.topMargin = 0;
        } else if ((params.topMargin + bean.getWidth()) > getHeight()) {
            params.topMargin = getHeight() - bean.getHeight();
        }

        view.setTag(x + "tag" + y);
        ((CommonTagView) view).setData(bean, signature);
        this.addView(view, params);
        View finalView = view;
        //每个添加的TagView设置点击监听事件
        finalView.setOnClickListener(v -> {
            CommonTagView tagView = (CommonTagView) v;
            String tag = (String) tagView.getTag();

        });
    }


}
